﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NetDon.Utilities;
using NetDon.WebControls;
using NetDon.eBoss.Data.CollectionClasses;
using NetDon.eBoss.Data.EntityClasses;
using NetDon.eBoss.Data.HelperClasses;
using NetDon.eBoss.Portal;
using NetDon.eBoss.Portal.HtmlTool;

namespace NetDon.OA.Portal.HtmlTool
{
    [Navigation("Html工具", "生成Html")]
    public partial class HtmlToolAccountManagement : NetDonPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                PageQueryAdapter1.PerformQuery();

            }
        }



        protected void PageQueryAdapter1_PerformGetCondition(object sender, PerformGetConditionEventArgs e)
        {
            var criteria = new HtmlAccountCriteria
            {



            };

            e.Condition = criteria;
        }

        protected void PageQueryAdapter1_PerformGetRecordCount(object sender, PerformGetRecordCountEventArgs e)
        {
            e.RecordCount = new HtmlToolAccountService().GetHtmlToolAccountCount((HtmlAccountCriteria)e.Condition);
        }

        protected void PageQueryAdapter1_PerformGetPageData(object sender, PerformGetPageDataEventArgs e)
        {

            e.DataSource = new HtmlToolAccountService().SearchHtmlToolAccountByCondition((HtmlAccountCriteria)e.Condition, gridCustomerInfoList.SortExpression,
                                                      gridCustomerInfoList.SortDirection, e.PageSize, e.PageIndex);
        }


        protected void btnReload_Click(object sender, EventArgs e)
        {
            PageQueryAdapter1.PerformQuery();
        }


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            PageQueryAdapter1.PerformQuery();
        }

        protected void gridOrderList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var entity = (e.Row.DataItem);

                var orderId = e.Row.Attributes["Id"] = Convert.ToString(DataBinder.Eval(entity, "Id"));


            }
        }

        protected void btnDeleteDemand_Click(object sender, EventArgs e)
        {
            var trans = new Transaction(IsolationLevel.ReadCommitted, "btnDeleteHtmlToolAccount");
            try
            {
                var accountid = Convert.ToInt32(fieldCustomerId.Value);
           
                //check if used by other account
                var account = new HtmltoolaccountEntity(accountid);
                if (account.Isdefault.HasValue && account.Isdefault.Value ==1)
                {
                    Alert("默认账号不可以删除!");
                    return;
                }
                var referedAccounts = new HtmltoolaccountCollection();
                referedAccounts.GetMulti(HtmltoolaccountFields.From == accountid);
                if (referedAccounts.Count > 0)
                {
                    Alert("该帐号被其它账号引用，不可以删除。请先删除其它账号!");
                    return;
                }

                var productGroups = new ProductgroupCollection();
                productGroups.GetMulti(ProductgroupFields.Accountid == accountid);
                foreach (var productGroup in productGroups)
                {

                    trans.Add(productGroup);
                    productGroup.Delete();
                }
               

                var styles = new HtmltoolaccounttemplateCollection();
                styles.GetMulti(HtmltoolaccounttemplateFields.Accountid == accountid);
                foreach (var style in styles)
                {

                    trans.Add(style); style.Delete();
                }
               
                trans.Add(account);
                account.Delete();
                trans.Commit();
                PageQueryAdapter1.PerformQuery();
                Alert("删除成功!");
            }
            catch (Exception)
            {
                trans.Rollback();
                Alert("删除失败！");
                throw;
            }

        }



    }
}